//
//  PostReplyPictureCell.m
//  sensen_ios
//
//  Created by mc on 16/3/26.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostReplyPictureCell.h"
#import "BigImageViewController.h"
#import "ZKTextImgReplaceHandle.h"

#define Margin 13
#define LeftMargin 16
@implementation PostReplyPictureCell

+(instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString * cellIdentifier = @"PostReplyPictureCell";
    PostReplyPictureCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
    
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self =  [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

    


- (void)setPostModel:(PostModel *)postModel{
    
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
      _postModel = postModel;
}


- (void)setPostReplyModel:(PostReplyModel *)postReplyModel{
    _array = postReplyModel.img;
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH)];
//    [self addSubview:imageView];
//    imageView.image = [UIImage imageNamed:@"论坛_我的帖子_回复"];
    imageView.image = [[UIImage imageNamed:@"论坛_我的帖子_回复"] resizableImageWithCapInsets:UIEdgeInsetsMake(30, 110, 20, 200)
                                                                         resizingMode:UIImageResizingModeStretch];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [self.contentView addSubview:imageView];

    //imageView.image = [[UIImage imageNamed:@"论坛_我的帖子_回复"] stretchableImageWithLeftCapWidth:300  topCapHeight:40];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(-5);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
    }];
    
    
    
    
    NSString * string =  [NSString stringWithFormat:@"回复帖子：%@", postReplyModel.post_message];
    //CGFloat   number = [string  getBoundingRectOfStringSize:CGSizeMake(SCREEN_WIDTH - 26, 10000) font:12].size.height;
    CGFloat number = [ZKTextImgReplaceHandle getImgTextHeightWithText:string regex:nil size:CGSizeMake(SCREEN_WIDTH - 26, 10000) fontSize:12];
    
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, SCREEN_WIDTH - 32, number)];
    [self.contentView addSubview:_lable];
    _lable.textColor = [UIColor darkGrayColor];
    _lable.font = FONT(16);
    _lable.numberOfLines = 0;
    

    NSMutableAttributedString *attr = [ZKTextImgReplaceHandle handleText:string withRegex:nil];
    if (attr) {
        _lable.attributedText = attr;
    } else {
        _lable.text = string;
    }

    
    
    for (int i = 0; i < _array.count; i++) {
        
        CGFloat width = (SCREEN_WIDTH - Margin * 2 - LeftMargin * 2) / 3;
        CGFloat x = LeftMargin + Margin * (i % 3 ) + width * (i % 3) ;
        CGFloat y = Margin * (i / 3) + width * (i / 3);
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y + 15 + number, width, width)];
        __strong UIImageView *s_view = imageView;
        [imageView setImageWithURL:_array[i] placeholderImage:DEFAULT_CAROUSEL_IMAGE completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            // 设置正确的图片尺寸
            s_view.image = [image getCenterImageWithRatio:1]; // 根据宽高比1：1获得图片
        }];

        [self.contentView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        imageView.tag = i + 100;
    }
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    BigImageViewController *vc = [[BigImageViewController alloc] init];
    vc.imageData = _array;
    NSInteger index = tap.view.tag - 100;
    vc.indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.viewController presentViewController:vc animated:YES completion:nil];
}


@end
